package UILayer;
import java.util.Scanner;
import CtrLayer.ProductCtr;
import ModLayer.ProductCon;
import ModLayer.Product;
import ModLayer.TimeAndCheck;
import java.util.ArrayList;

/**
 * Write a description of class ProductUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProductUI
{
    private ProductCtr productCtr;
    private TimeAndCheck time;
    
    public ProductUI()
    {
        productCtr = productCtr.getInstance();
        time = time.getInstance();
    }

    public void start() 
    { 
        boolean finished = false; 
        while(!finished)
        {
            printMenu();
            switch(reader(4)) 
            { 
                case "0": finished = true; break; 
                case "1": removeProduct(); break; 
                case "2": addProduct(); break; 
                case "3": updateProduct(); break;
                case "4": search(); break;
            } 
        } 
    }

    private void printMenu()
    { 
        System.out.println("\fWhat would you like to do, Steve?"); 
        System.out.println("(0) Back"); 
        System.out.println("(1) Remove product"); 
        System.out.println("(2) Add product"); 
        System.out.println("(3) Update product");
        System.out.println("(4) Search Items");
        System.out.println("Make your choice: "); 
    }

    public void updateProduct()
    {
        System.out.println("\f*** Update product ***");
        System.out.println("Type in product id: ");
        String prID = reader2();
        boolean finished = false;
        Product pr = productCtr.findProduct(prID);
        if(pr != null)
        {
            while(!finished)
            {
                System.out.println("\f*** Update product ***");
                System.out.println("Name: " +pr.getName());
                System.out.println("ID: " +pr.getID());
                System.out.println("Description: " +pr.getDescription());
                System.out.println("Price: " +pr.getPrice(pr.getDiscount()));
                System.out.print("Available to ");
                if(pr.getSellable() == true)
                    System.out.print("buy ");
                if(pr.getLeaseable() == true&&pr.getSellable() == true)
                    System.out.print("and ");
                else
                    System.out.print(".");
                if(pr.getLeaseable() == true)
                    System.out.print("lease.");
                System.out.println();
                System.out.println();
                System.out.println("\nWhat would you like to update?");
                System.out.println("(0) Back.");
                System.out.println("(1) Name.");
                System.out.println("(2) Sellable or leasable.");
                System.out.println("(3) Description.");
                System.out.println("(4) Price.");
                System.out.println("(5) Lease Price");
                System.out.println("(6) Pallout");
                System.out.println("Make your choice: ");
                String line;
                switch(reader(6)) 
                {
                    case "0": finished = true; break; 
                    
                    case "1":
                    System.out.println("\fType in new name of product:");
                    line = reader2();
                    pr.setName(line);
                    break;

                    case "2":
                    boolean sell, lease;
                    System.out.println("\nIs it for sale?");
                    if(reader2().startsWith("y"))
                        sell = true;
                    else
                        sell = false;
                    System.out.println("\nIs it for lease?");
                    if(reader2().startsWith("y"))
                        lease = true;
                    else
                        lease = false;
                    pr.setSellable(sell);
                    pr.setLeaseable(lease);
                    break;

                    case "3":
                    System.out.println("\fType in new description of product:");
                    line = reader2();
                    pr.setDescription(line);
                    break;

                    case "4":
                    System.out.println("\fType in new price of product:");
                    double line2 = Double.parseDouble(reader2());
                    pr.setPrice(line2);
                    break;
                    
                    case "5":
                    System.out.println("\fType in new leasing price of product:");
                    double line3 = Double.parseDouble(reader2());
                    pr.setLeasePrice(line3);
                    break;
                    
                    case "6":
                    System.out.println("\fType in new pallet of product:");
                    int line4 = Integer.parseInt(reader2());
                    pr.setPallet(line4);
                    System.out.println("\fType in price of pallot:");
                    double line5 = Double.parseDouble(reader2());
                    pr.setPalletDiscount(line5);
                    break;
                }
            }
        }
    }

    public void addProduct()
    {
        boolean finished = false; 
        System.out.println("\f*** Add product ***");
        System.out.println("Type in name of the product.");
        String name = reader2();
        System.out.println("\nType in description of product.");
        String description = reader2();
        System.out.println("\nWhat is its price?");
        String price = reader2();
        while(time.checkStringToDouble(price)==0)
        {
            System.out.println("\nTry once again?");
            price = reader2();
        }
        double m = Double.parseDouble(price);
        System.out.println("\nWhat is its leasing price?");
        price = reader2();
        while(time.checkStringToDouble(price)==0)
        {
            System.out.println("\nTry once again?");
            price = reader2();
        }
        double m2 = Double.parseDouble(price);
        System.out.println("\nIs it for sale?");
        boolean sell, lease;
        if(reader2().startsWith("y"))
            sell = true;
        else
            sell = false;
        System.out.println("\nIs it for lease?");
        if(reader2().startsWith("y"))
            lease = true;
        else
            lease = false;
        
        Product pr = productCtr.findProduct(time.getNoForCustomer());
        System.out.println("\nHow much is in the DIY centre?");
        price = reader2();
        while(time.checkStringToDouble(price)==0)
        {
            System.out.println("\nTry once again?");
            price = reader2();
        }
        int stockDIY=Integer.parseInt(price);
        System.out.println("\nHow much is in the Bathroom?");
        price = reader2();
        while(time.checkStringToDouble(price)==0)
        {
            System.out.println("\nTry once again?");
            price = reader2();
        }
        int stockBath=Integer.parseInt(price);
        System.out.println("\nHow much is in the Kitchen?");
        price = reader2();
        while(time.checkStringToDouble(price)==0)
        {
            System.out.println("\nTry once again?");
            price = reader2();
        }
        int stockKit=Integer.parseInt(price);
        productCtr.createProduct(name, description, sell, lease, m, m2,
        stockDIY, stockBath, stockKit);
        System.out.println("\fProduct was created.");
    }

    public void removeProduct()
    {
        System.out.println("\f***Remove a product***");
        System.out.println("Enter product id");
        String read = reader2();
        if(read != "0")
        {
            if(productCtr.findProduct(read)!=null)
            {
                productCtr.deleteProduct(read);
                System.out.println("\nProduct successfully deleted.");
            }
            else
                System.out.println("\nNo such Customer was found.");
        }
    }

    private String reader2()
    { 
        Scanner command = new Scanner(System.in);
        System.out.print(">> ");
        String c = command.nextLine();
        return c;
    }

    private String reader(int limitIndex)
    {
        final String[] validNumbers = {"0","1","2","3","4","5","6","7","8","9"};
        Scanner command = new Scanner(System.in);
        String c = null;
        boolean finished = false;
        while(!finished)
        {
            System.out.print("\n> ");
            c = command.nextLine();
            for(int i=0; i<=limitIndex; i++)
            {
                if(validNumbers[i].equals(c))
                    finished = true;
            }
            if(finished == false)
            {
                System.out.print("\nTry once again");
                c = null;
            }
        }
        return c;
    }

    public void search()
    {
        boolean finished = false;
        int ok = 0;
        System.out.println("\f*** Search menu ***");
        for(Product x: productCtr.searchProduct())
            printProduct(x);
        pause();
    }

    public void printProduct(Product pr)
    {
        System.out.println("*****");
        System.out.println("Name of the product: " +pr.getName());
        System.out.println("ID of the product: " +pr.getID());
        System.out.println("Description of the product: "+pr.getDescription());
        System.out.println("Number in stock: " +pr.getStock());
        System.out.println("Number in Kitchen: " +pr.getStockKit());
        System.out.println("Number in Bathroom: " +pr.getStockBath());
        System.out.println("Number in the DIY centre: " +pr.getStockDIY());
        System.out.println("Price of the product: "+pr.getPrice(pr.getDiscount())+ " DKK");
        System.out.println("Can be bought in numbers of "+pr.getPallet()+ " for the price of "+ pr.getPalletDiscount()+ " DKK");
        System.out.print("Available to ");
        if(pr.getSellable() == true)
            System.out.print("buy ");
        if(pr.getLeaseable() == true&&pr.getSellable() == true)
            System.out.print("and ");
        else
            System.out.print(".");
        if(pr.getLeaseable() == true)
            System.out.print("lease.");
        System.out.println();
    }
    
    private void pause()
    {
        boolean finished = false; 
        while(!finished)
        {
            System.out.println("\nPress something to back.");
            if(reader2().startsWith(""))
                finished = true;
        }
    }
}